package org.example.controller;

import com.github.pagehelper.PageHelper;
import org.example.model.TransferRecord;
import org.example.service.TransferRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author lijun
 * @date 2025-10-20 11:53
 */
@RestController
@RequestMapping("/transfer")
public class TransferRecordController {

    @Autowired
    private TransferRecordService transferRecordService;

    @GetMapping("/list")
    public ResponseEntity<List<TransferRecord>> getAllRecords( @RequestParam(defaultValue = "1") int pageNum,
                                                               @RequestParam(defaultValue = "50") int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<TransferRecord> records = transferRecordService.getAllRecords();
        return ResponseEntity.ok(records);
    }

    @GetMapping("/{id}")
    public ResponseEntity<TransferRecord> getRecordById(@PathVariable Integer id) {
        TransferRecord record = transferRecordService.getRecordById(id);
        if (record != null) {
            return ResponseEntity.ok(record);
        } else {
            return ResponseEntity.notFound().build();
        }
    }
    @GetMapping("/queryCache/{id}")
    public ResponseEntity<TransferRecord> queryCache(@PathVariable Integer id) {
        TransferRecord record = transferRecordService.queryCache(id);
        if (record != null) {
            return ResponseEntity.ok(record);
        } else {
            return ResponseEntity.notFound().build();
        }
    }
    @PostMapping("/add")
    public ResponseEntity<String> addRecord(@RequestBody TransferRecord record) {
        boolean success = transferRecordService.addRecord(record);
        if (success) {
            return ResponseEntity.ok("添加成功");
        } else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("添加失败");
        }
    }

    @PutMapping("/update")
    public ResponseEntity<String> updateRecord(@RequestBody TransferRecord record) {
        boolean success = transferRecordService.updateRecord(record);
        if (success) {
            return ResponseEntity.ok("更新成功");
        } else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("更新失败");
        }
    }

    @DeleteMapping("/delete/{id}")
    public ResponseEntity<String> deleteRecord(@PathVariable Integer id) {
        boolean success = transferRecordService.deleteRecord(id);
        if (success) {
            return ResponseEntity.ok("删除成功");
        } else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("删除失败");
        }
    }

    @GetMapping("/search")
    public ResponseEntity<List<TransferRecord>> searchRecords(
            @RequestParam(required = false) String channel,
            @RequestParam(required = false) String type,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date startTime,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endTime) {
        List<TransferRecord> records = transferRecordService.searchRecords(channel, type, startTime, endTime);
        return ResponseEntity.ok(records);
    }
}
